﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SQLDLL;
using DLL;
using DevExpress.XtraEditors;

namespace NursingDocumentInfoSystem
{
    public partial class NursingMeasure2 : Form
    {
        public string ID = "";
        public string DID = "";
        public NursingMeasure2(string id, string departmentID)
        {
            InitializeComponent();
            ID = id;
            DID = departmentID;
            StartPosition = FormStartPosition.CenterScreen;
            if (ID == "-1")
            {
                Text = "新增模板";
            }
            else
            {
                Text = "修改模板";
                string sql = "SELECT text1  FROM[BNNursingDocsSystem_Develop].[dbo].[NursingMeasure](NOLOCK) where ID = '" + ID + "'";
                textBox1.Text = SqlHelper.ExecuteScalar(CommandType.Text, sql).ToString();
                sql = "SELECT text2  FROM[BNNursingDocsSystem_Develop].[dbo].[NursingMeasure](NOLOCK) where ID = '" + ID + "'";
                richE_ContentInfor.Text = SqlHelper.ExecuteScalar(CommandType.Text, sql).ToString();
                CheckEdit();
            }
        }
        /// <summary>
        /// 关闭窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        /// <summary>
        /// 提交数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Submit_Click(object sender, EventArgs e)
        {
            if (ID == "-1") //新增
            {
                string sql = "  insert into [BNNursingDocsSystem_Develop].[dbo].[NursingMeasure] (DepartmentID,text1,text2,ifuse) values ('" + DID + "','" + textBox1.Text + "','" + richE_ContentInfor.Text + "','1')";
                SqlHelper.ExecuteNonQuery(CommandType.Text, sql);
                DialogResult = DialogResult.OK;
            }
            else
            {
                if (!CheckEdit()) return;
                string sql = "update [BNNursingDocsSystem_Develop].[dbo].[NursingMeasure] set text1 ='" + textBox1.Text + "',text2 ='" + richE_ContentInfor.Text + "' where ID ='" + ID + "'";
                SqlHelper.ExecuteNonQuery(CommandType.Text, sql);
                DialogResult = DialogResult.OK;
            }
        }

        /// <summary>
        /// 提示是否可以修改模板
        /// </summary>
        /// <returns></returns>
        private bool CheckEdit()
        {
            string exisql = string.Format("SELECT TOP 1 DepartmentID FROM [BNNursingDocsSystem_Develop].[dbo].[NursingMeasure](NOLOCK) WHERE ID={0} ", ID);
            object objdepId = SqlHelper.ExecuteScalar(exisql);
            if (objdepId == DBNull.Value) { XtraMessageBox.Show("要修改的模板不存在"); return false; }
            if (objdepId == null) { XtraMessageBox.Show("要修改的模板不存在"); return false; }
            if (string.IsNullOrEmpty(objdepId.ToString())) { XtraMessageBox.Show("要修改的模板不存在"); return false; }
            if (!objdepId.ToString().Equals(DID)) { XtraMessageBox.Show("不能修改其他科室的模板"); return false; }
            return true;
        }
    }
}
